package com.yb.xczx.cloud.resourse.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * Copyright (C), 2022-2023, 姚兵
 * Author: 32210
 * Date: 2023/5/14 12:19
 * FileName: IndexController
 * Description:
 */

//@RestController
@Slf4j
public class IndexController {

    @Autowired
    RestTemplate restTemplate;

    @RequestMapping("")
    public String index(){
        log.error("访问index");
        return "index";
    }

//    @RequestMapping("authorized")
//    public String authorized(Model model,@RequestParam(required = false) String code,@RequestParam String state,@RequestBody(required = false)  String requestBody){
//        model.addAttribute("code",code);
//        model.addAttribute("body",requestBody);
//        log.info("code:{}",code);
//        log.info("requestBody:{}",requestBody);
//        //设置请求头参数
//        HttpHeaders httpHeaders = new HttpHeaders();
//        httpHeaders.add("Authorization","Basic bWVzc2FnaW5nOm1lc3NhZ2luZw==");
//        httpHeaders.add("Cookie",state);
//        httpHeaders.add("Content-Type","application/x-www-form-urlencoded");
////        code={{authCode}}&grant_type=authorization_code&redirect_uri=http://127.0.0.1/authorized&scope=message.read
//        //设置body参数
//        MultiValueMap<String, String> multiValueMap = new LinkedMultiValueMap<>();
//        multiValueMap.add("code",code);
//        multiValueMap.add("grant_type","authorization_code");
//        multiValueMap.add("redirect_uri","http://localhost:8080/authorized");
//        multiValueMap.add("scope","message.read%20message.write");
////        Authorization: {{Authorization}}
////        Cookie: {{cookie}}
////        Content-Type: application/x-www-form-urlencoded
//        HttpEntity<Object> httpEntity = new HttpEntity<>(multiValueMap,httpHeaders);
//        ResponseEntity<Map> responseEntity = restTemplate.postForEntity("http://localhost/oauth2/token?state="+state,
////                httpEntity, Map.class);
//        Map body = responseEntity.getBody();
//        log.info("authorized body:{}",body);
//        return "toAuth";
//    }

    @RequestMapping("token")
    public String token(Model model,@RequestBody String token){
        model.addAttribute("token",token);
        return "token";
    }

    @RequestMapping("hello.html")
    public String helloHtml(){

        return "hello";
    }

    @RequestMapping("/access/token")
    public String accessToken(@RequestBody String body,Model model){
        model.addAttribute("body",body);
        return "accessToken";
    }
}
